package homework7.soso.service.impl;

import homework7.soso.entity.MobileCard;
import homework7.soso.service.NetService;

/**
 * @author fengq
 * 网虫套餐
 */
public class NetPackage extends ServicePackage implements NetService {
    /**
     * 上网流量
     */
    private int flow;


    @Override
    public void showInfo() {
        System.out.println("网虫套餐:上网流量3GB/月");
    }

    public NetPackage(int flow) {
        this.flow = flow;
    }

    public int getFlow() {
        return flow;
    }

    public void setFlow(int flow) {
        this.flow = flow;
    }

    public NetPackage(double price, int flow) {
        this.price=price;
        this.flow = flow;
    }


    @Override
    public int netPlay(int flow, MobileCard card) throws Exception {
        NetPackage netPackage = (NetPackage) card.getServicePackage();
        int spend = flow*1024;
        ;
        for (int i = 0; i < flow*1024; i++) {
            if ((netPackage.getFlow())*1024-card.getRealFlow()>0) {

                card.setRealFlow(card.getRealFlow()+1);
            }else if (card.getMoney()>=0.1) {
                card.setMoney(card.getMoney()-0.1);
                card.setConsumeAmount(card.getConsumeAmount()+0.1);
            }else {
                spend = i;
                card.setMoney(spend);
                throw new Exception("本次上网话费"+spend+"MB，您的余额不足，请充值后使用");
            }

        }
        return spend;
    }
}
